//
//  HomeCell.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "HomeCell.h"
#import "GoodsCell.h"
@interface HomeCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (assign, nonatomic) CGFloat cellW;
@property (assign, nonatomic) CGFloat cellH;
@end
@implementation HomeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.cellW = (SCREEN_WIDTH - 20 * 3) / 2;
    self.cellH = 240 / 168.0 * self.cellW;
    [self.collectionView registerNib:[GoodsCell nib] forCellWithReuseIdentifier:[GoodsCell ID]];
    self.array = [GoodsTool find:@"随机" count:arc4random()%10 + 2];
    
}
- (void)setArray:(NSArray<Goods *> *)array{
    _array = array;
    self.collectionH.constant = ceil(self.array.count / 2.0) * (self.cellH + 20) + 20;
    self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, self.collectionH.constant);
    [self.collectionView reloadData];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[GoodsCell ID] forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BuyViewController *vc = [[BuyViewController alloc] init];
    vc.model = self.array[indexPath.row];
    [[self currentVC].navigationController pushViewController:vc animated:YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.cellW, self.cellH);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

@end
